<template>
  <view>
    <u-navbar
      v-if="!$isWeixin()"
      :customBack="backToHome"
      :title="title"
      :titleBold="true"
      :background="{ backgroundColor: '#ffffff' }"
      :is-fixed="true"
    ></u-navbar>
    <u-tabs active-color="#FF7A23" :list="tabList" :current="tabIndex" @change="index => (tabIndex = index)"></u-tabs>

    <view v-if="tabIndex == 0"><relations :list="This_Dealer_RelationList"></relations></view>

    <view class="" v-if="tabIndex == 1">
      <u-cell-item title="一级经销商名称" :arrow="false" :value="First_Dealer_BaseInfo.Name"></u-cell-item>
      <u-cell-item
        :border-bottom="false"
        title="二级经销商名称"
        :arrow="false"
        :value="Double_Dealer_BaseInfo.Name"
      ></u-cell-item>

      <store-information :data="Door_Dealer_BaseInfo"></store-information>
    </view>

    <view class="" v-show="tabIndex == 2"><store-photo-form ref="storePhotoForm"></store-photo-form></view>

    <view v-show="tabIndex == 3"><apply-form ref="applyForm"></apply-form></view>

    <view class="btn-group-fixed">
      <u-button :disabled="btnDisabled" type="warning" @click="submit">保存提交</u-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      btnDisabled: false,
      ShowLevel: null,
      title: '门店照片维护',

      tabList: [{ name: '销售业务关系' }, { name: '基本信息' }, { name: '门店照片' }, { name: '申请备注' }],
      tabIndex: 0,

      relationsMetaData: {},

      DealerAuditInfo: {},
      DoorPhotoAuditInfo: {},
      DoorPhotoList: [],
      Door_Dealer_BaseInfo: {},
      Double_Dealer_BaseInfo: {},
      First_Dealer_BaseInfo: {},
      This_Dealer_OtherInfo: {},
      This_Dealer_RelationList: [],

      form: {
        DealerId: null,
        PhotoList: [],
        Remake: '',
        Date: null
      }
    };
  },

  onLoad(options) {
    if (options.Level) {
      this.ShowLevel = options.Level;
    }
    if (options.DealerId) {
      this.form.DealerId = options.DealerId;
    }

    this.init();
    this.$setTitle(this.title);
  },

  methods: {
    backToHome() {
      uni.navigateBack();
    },

    submit() {
      let me = this;
      me.btnDisabled = true;

      const applyForm = this.$refs.applyForm.form;
      const storePhotoForm = this.$refs.storePhotoForm;

      this.form.Date = applyForm.ApplyTime;
      this.form.Remake = applyForm.Remark;
      this.form.PhotoList = storePhotoForm.list;

      console.log(this.form);

      this.$u.api.updateDoorPhotoApply(this.form).then(res => {
        const { ResultCode, ResultMessage } = res;
        if (ResultCode === 1) {
          this.$u.toast(ResultMessage);
          setTimeout(() => {
            uni.navigateBack();
          }, 1000);
        } else {
          me.btnDisabled = false;
        }
      });
    },

    init() {
      this.$u.api
        .getInitInformationUpdateDetail({
          DealerId: this.form.DealerId
        })
        .then(res => {
          console.log(res);
          const {
            ResultCode,
            ShowLevel,
            DealerAuditInfo,
            DoorPhotoAuditInfo,
            DoorPhotoList,
            Door_Dealer_BaseInfo,
            Double_Dealer_BaseInfo,
            First_Dealer_BaseInfo,
            This_Dealer_OtherInfo,
            This_Dealer_RelationList,
            DirectorList,
            SalseList,
            DealerTerminalList,
            TeamList,
            SeniorDealerTerminalList,
            DealerDivisionList,
            RegionalDirectorList,
            SalesServiceList
          } = res;

          if (ResultCode === 1) {
            this.ShowLevel = ShowLevel;

            if (This_Dealer_RelationList) {
              this.This_Dealer_RelationList = This_Dealer_RelationList;
            }

            if (DealerAuditInfo) {
              this.DealerAuditInfo = DealerAuditInfo;
            }

            if (DoorPhotoAuditInfo) {
              this.DoorPhotoAuditInfo = DoorPhotoAuditInfo;
            }

            if (DoorPhotoList) {
              this.DoorPhotoList = DoorPhotoList;
            }

            if (Door_Dealer_BaseInfo) {
              this.Door_Dealer_BaseInfo = Door_Dealer_BaseInfo;
            }

            if (Double_Dealer_BaseInfo) {
              this.Double_Dealer_BaseInfo = Double_Dealer_BaseInfo;
            }

            if (First_Dealer_BaseInfo) {
              this.First_Dealer_BaseInfo = First_Dealer_BaseInfo;
            }

            if (This_Dealer_OtherInfo) {
              this.This_Dealer_OtherInfo = This_Dealer_OtherInfo;
            }

            this.$refs.storePhotoForm.initPhotoList(DoorPhotoList);
          }
        });
    }
  }
};
</script>

<style lang="scss"></style>
